package com.rich.action;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.rich.domain.PageBean;
import com.rich.domain.User;
import com.rich.service.UserService;

@Controller
@Scope("prototype")
public class UserAdminAction extends ActionSupport implements ModelDriven<User>{
	private User user = new User();

	public User getModel() {
		return user;
	}
	
	@Resource
	private UserService userService;

	//获取当前页
	private Integer pageNumber;

	public void setPageNumber(Integer pageNumber) {
		this.pageNumber = pageNumber;
	}

	/**
	 * 查找所用用户
	 * @return
	 */
	public String findAll(){
		PageBean<User> pageBean = userService.findByPage(pageNumber);
		ActionContext.getContext().getValueStack().set("pageBean", pageBean);
		return "findAll";
	}
	
	/**
	 * 删除用户
	 * @return
	 */
	public String delete(){
		User existUser = userService.findByUid(user.getUid());
		userService.delete(existUser);
		return "deleteSuccess";
	}
	
	/**
	 * 编辑用户
	 * @return
	 */
	public String edit(){
		user = userService.findByUid(user.getUid());
		return "editSuccess";
	}
	
	/**
	 * 更新用户
	 * @return
	 * @throws Exception
	 */
	public String update() throws Exception{
		userService.update(user);
		return "updateSuccess";
	}
}
